#include <bitset>
#include <iostream>
#include <string>
using namespace std;

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

  int T;
  for (cin >> T; T; T--) {
    int sum = 0;
    bitset<9> vis = 0;
    string s;

    cin >> s;
    for (int i = 0; i < s.size(); i++) {
      sum += s[i] - '0';
    }
    sum %= 9;
    vis[sum] = 1;
    for (int i = 0; i < s.size() && !vis[0]; i++) {
      int t = s[i] - '0';
      if (t == 2 || t == 3) {
        int dt = t * (t - 1);

        vis |= (vis << dt) | (vis >> (9 - dt));
      }
    }
    cout << (vis[0] ? "YES\n" : "NO\n");
  }

  return 0;
}